package com.compass.doctor.ui.mine

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.banner.annotation.Visibility
import com.compass.doctor.databinding.ActivityIntegralBinding
import com.compass.doctor.dialog.WithdrawExplainDialog
import com.compass.doctor.ui.mine.adapter.WalletDetailAdapter
import com.compass.doctor.ui.mine.viewmodel.IntegralViewModel
import com.compass.doctor.base.BaseVBActivity
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.model.Wallet
import com.compass.framework.utils.StatusBarSettingHelper
import com.compass.framework.utils.dpToPx

/**
 * Description： 积分activity  我的钱包
 * Date:2023/11/17 9:39
 */
class IntegralActivity : BaseVBActivity<ActivityIntegralBinding>() {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, IntegralActivity::class.java)
            context.startActivity(intent)
        }
    }

    private lateinit var walletDetailAdapter: WalletDetailAdapter

    private val integralViewModel: IntegralViewModel by viewModels()

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarSettingHelper.setStatusBarTranslucent(this)
        StatusBarSettingHelper.statusBarLightMode(this@IntegralActivity, true)
        mBinding.tvTitleBar.setRightText("联系客服")

        mBinding.tvTitleBar.getRightTextView().onClick {
            val intent = Intent()
            intent.action = Intent.ACTION_DIAL
            intent.data = Uri.parse("tel:18165375386")
            startActivity(intent)
        }

        walletDetailAdapter= WalletDetailAdapter()
        mBinding.recyclerView.apply {
            layoutManager=LinearLayoutManager(this@IntegralActivity)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds( top = dpToPx(10), bottom = dpToPx(10))
                setLastBottom(true)
            })
            adapter=walletDetailAdapter
        }
        walletDetailAdapter.onItemClickListener={_, position ->
            val item = walletDetailAdapter.getItem(position)
            IncomeDetailsActivity.start(this,item)
        }
        mBinding.tvExplain.onClick {
            integralViewModel.getWithdrawExplain()

        }
        mBinding.tvMore.onClick {
            WalletDetailActivity.start(this)
        }
        mBinding.tvWithdrawal.onClick {
            WithdrawActivity.start(this, integralViewModel.walletLiveData.value?.balanceMoney)
        }
        mBinding.tvBankCard.onClick {
            BankCardActivity.start(this)
        }

    }

    override fun initData() {
        integralViewModel.walletLiveData.observe(this) {
            it?.let {
                setWalletData(it)
            }
        }
        integralViewModel.walletListLiveData.observe(this) {
            if(it.isNullOrEmpty()){ mBinding.emptyView.visibility = View.VISIBLE }
            walletDetailAdapter.setData(it)
        }
        integralViewModel.explainLiveData.observe(this){
            it?.let {explain->
                WithdrawExplainDialog.Builder(this,explain).show()
            }

        }
        integralViewModel.getDoctorWallet()
        //这里加载第一页数据即可
        integralViewModel.getDoctorWalletDetail(1)
    }


    private fun setWalletData(wallet: Wallet) {
        mBinding.apply {
            tvBalanceMoney.text = String.format("%.2f", wallet.balanceMoney)
            tvFreezeMoney.text = String.format("%.2f", wallet.freezeMoney)
            tvAuditMoney.text = String.format("%.2f", wallet.auditMoney)
        }
    }


}